<?php

class User_AccountController extends Custom_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function dashboardAction()
    {
     	#set submenu
		$this->_setTab(2);
    }

    public function changepasswordAction()
    {
        $form = new User_Form_ChangePassword();
        $this->view->form = $form;

        if($this->getRequest()->isPost())
        {
            $formData = $this->getRequest()->getPost();

            if($form->isValid($formData))
            {
				$user = new User_Model_DbTable_User();
				$user_id = Zend_Auth::getInstance()->getIdentity()->id;
				
				$data['password'] = Custom_General::encrypt($formData['password']);
				$res = $user->updateById($data,$user_id);

				if($res)
				{
					$this->addMessage('Password changed successfully, please login with new password');
					Zend_Auth::getInstance()->clearIdentity();
					$this->Redirector('login','auth','user');
				}
				else if($user->getError())
				{
					$this->view->msg = $error = $user->getError();
				}
            }
		}
		
		#set submenu
		$this->_setTab(1);
    }

    public function indexAction()
    {
       	$user		= new User_Model_DbTable_User();
		$user_id	= $this->auth->getIdentity()->id;
		$form		= new User_Form_UserProfile($user_id);
		$user_info	= $user->getOne($user_id);
		
		if($this->getRequest()->isPost())
        {
            $formData = $this->getRequest()->getPost();
			
            if($form->isValid($formData))
            {
				$user_data = $form->getValues();
                if(empty($user_data['image'])) unset ($user_data['image']);
				$res = $user->updateById($user_data,$user_id);
              
				if($res) 
					$this->addMessage('Profile updated successfully');
				else if($user->getError())
					$this->addMessage($user->getError(),'error');

				$this->Redirector('index','account','user');
		   }
		}

		$form->populate($user_info);
        $this->view->form = $form;
		
		#set submenu
		$this->_setTab(0);
	}

	public function infoAction()
	{
		$uid = $this->_getParam('uid');

		if($uid)
		{
			$email = Custom_General::decode($uid);

			$objUser	= new User_Model_DbTable_User();
			$objProfile	= new User_Model_DbTable_Profile();
		
			$user		= $objUser->getOneByColumn('email', $email, array('id','first_name','last_name','email'));
			$profile	= $objProfile->getOneByColumn('user_id',$user['id']);
			
			$data['User'] = $user;
			$data['Profile'] = $profile;
			$data['Profile']['country'] = $country['printable_name'];

			$this->view->data = $data;
			$this->view->profileImagePath = $this->Config->params->user->profileImagePath;
		}
		else
		{
			$this->view->data = false;
		}
	}

	private function _setTab($activeIndx)
	{
		$subItems = array(__('Manage Account'),__('Change Password'),__('Dashboard'));
		$subItemLinks = array('user/account','account/changepassword','user/dashboard');
		$linkRoutes = array('','cpassword','dashboard');

		$menues = array('0','1','2');

		if(!in_array($activeIndx, $menues)) $menues[] = $activeIndx;

		$subMenues = array('items' => $subItems, 'links'=> $subItemLinks,
						   'enableMenues' => $menues, 'activeIndx' => $activeIndx);

		$this->view->subMenues = $subMenues;
	}
}